C# Video to be added soon

To start of this tutorial I will go over some basic opening statements.


GraphicsDeviceManager simply states that we will be using that to manage our graphics.


SpriteBatch allows us to work with all of our sprites in one manageable group instead of everything separately. It just makes everything a lot easier.


This states that we will have our player be in our Gameobject engine. player is just a name I picked it can be anything I just for the sake of the tutorial put player. This technically you could just put in what you would name your player.


This states that we are creating a variable that is a integer, and that we are calling this integer "velocity". This can be named anything I just called it velocity because its relevant to what we will be doing with his variable and its easy to remember.


There is a lot here for a opening statement but I will break it down.
GamePadState is saying we are going to have something like a keyboard or controller that will be putting input into the game.
previousGamePadState is just a name to refer back to GamePadState;
We have a equals because this allows us to set up a reference to define this right from the get go and that is good.
GamePad.GetState sets it up so we will be receiving input from the GamePad.
(PlayerIndex.One) sets GamePad up so we know there is only one player we have to look for input from.

Now we should move down to the LoadContent void. If you already looked at the finished code you notice we skipped the Game1 and Initialize voids this because we already have all this code when we first make the project so there is nothing to add.


When we go to the LoadContent void it should look like this.

As you notice there is already a line in the LoadContent void. This line states that we replaced the old definition of spriteBatch with SpriteBatch being used with GraphicsDevice. This says this because all of our sprites will have a image so we use GraphicsDevice.
Note that it has the Name spriteBatch is equal to the Function Spritebatch with Graphics Device.



There is alot here, so again I will try to break it down.
Note player the Name not the Function is always set before the Equal sign.
Then we state that "player" equals new GameObject the Function of "player".
We state this because this is where we are loading a new GameObject or Sprite which ever you want to call it.

(Content.Load<Texture2D>("Sprites\\pixel"));
Content.Load shows that we are trying to take a file we put in our Content under our Project. This is shown in our Solution Explorer.
<Texture2D> that shows that our "player" will be a 2d texture, figure, image, ect.
("Sprites\\pixel") tells our program where to find this file we will access for this texture for our "player".
Since our files are in text we have to add Quotation Marks " "
We add the two slashes \\ because this tells our program our file is in a folder. If you look at my Solution Explorer you will see that my folder is "Sprites" and my file is "Pixle".
Then we add another closing parenthesis ) to close the first parenthesis opening up our Content.Load method.
Then finally adding a semicolon because that is how we close all of our C# statements.



This states where our "player" is first loaded.
Note this sets our Name of our sprite and uses .position which is what we set up in our GameObject.cs file.
We set this up so our "player's" position is a new Vector2. This shows that it is a new location that will be set in 2 dimensions (x,y).
After that notice that I have it set as ( , ). That is two coordinates(x (horizontal),y(Vertical)).
120, to start off with sets the position up so the X-coordinate is 120 pixels from the left of the window going right. It can be any number I just chose 120.
Note if the X-coordinate is Negative it will load to the left of your window so you would not be able to see it. The same would happen if the X-coordinate is too big it would load too much to the right and you would not see it.
Instead of graphics.GraphicsDevice.Viewport.Height - 80 you can set this up to any number for the Y-coordinate.
I set the Y-coordinate position so it is 80 less then the height of the window(Viewport). That is 80 counting up from the bottom.
You can do the same for the X coordinate. EX: graphics.GraphicsDevice.Viewport.Width - 80. That is 80 pixels counting from the right of the window left.
Notes: XNA will not tell you if your Coordinates are out of the windows view. we use parenthesis ( ) after Vector2 to make the x,y coordinates.
If it was Vector3 you can use (X,Y,Z), (Horizontal, Vertical, Depth), (Left & Right, Up & Down, Closer & farther away from you.
Here are some examples of Vector2 Coordinates: (1,1), (10,50), (100,173), (295, 113).

After adding this lets go down to our Update void.

Our first thing we will be adding to this void is this statement.


This states "velocity" is 5. We have to do this because any integer we make in C# when we first make it, it's default value is set to 0.


This statement sets up a couple things. It makes KeyboardState the constant have a variable that is set to "keyboardState". We have this so its equal to Keyboard.GetState(). This makes it so we find out what exactly is going on with our keyboard, like if a key is pressed.


This statement says a couple things I will try to break everything down. First we have our "If Statement". A If Statement is a statement that when whatever follows it in the parenthesis ( ) is followed. The things that follow the If Statement then whatever is inside the Curly Braces { } is applied to the Objects/Sprites/ect. that follows this voids If Statement.

So then for this If Statement when our keyboard has a key down, and that key is W, our "player's" Y-axis position is decreased by our velocity.

So in other words when the "W Key" is pressed wherever our player is, his position's Y-coordinate is decreased/minuses our velocity which we set as 5. So our "player's" position goes up by 5.

As you notice if you looked at the Final Product, all the following 3 other if statement voids are the same just with different keys and adding on the Y-axis, and the same on the X-axis.

Then We can move down to our Draw Void.


This states that where ever there is something not being drawn our GraphicsDevice fills the clear area with Black. When you first make a project it is set to CornFlowerBlue, you can set this color to any preference you want I just chose black.


This statement tells our project that our spriteBatch starts where we put our text. Note: Make sure everything that is in your spriteBatch that you are drawing comes after this statement.


spriteBatch.Draw simply states that we will be drawing something from our spriteBatch.
Then we add a open parenthesis ( to state that everything between the open and close parenthesis ( ) will be drawn.
player.sprite states that we will be drawing our "player's" sprite/image/object.
player.position states that we will be drawing whatever we want in our "player's" position. In this case we are drawing our "player's" sprite in the "player's" position.
This is followed with a null because the third statement in the draw call is what part of the image you want to draw so we set it as null so it draws all.
Color.White states that we will be drawing our "player" exactly how we imported its image. When we add a color instead of white it tints your image/sprite with whatever you set Color. to.
player.rotation states that we will be drawing our "player" to a certain rotation.
player.center states that we will be drawing our "player" at its center.
25.0f states how much we will be multiplying our "player's" size. Usually we can keep at 1f but it can be set to any number depending to how big you want our sprite to be. I set as 25 because in this tutorial I used a pixel as my sprite and that is really small so I had to multiply it a lot to be big.
SpriteEffects.None states that we will not effect our "player" with this you can flip your player horizontally, or vertically. This can come in handy later when making players and very time saving for spritemaps.
Then we close all that up with a closing parenthesis ) and finish it all off with a semicolon ;


Then we finish up our spriteBatch drawing with this statement.


Then we finish all of our drawing with this statement. Closing our Drawing Void.



Final Result





Download My Source Code Here with RapidShare.
Download
Free Web Hosting